package com.javabasic.algorithm.sword_of_offer;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see
 * @since Created by work on 2022/1/8 12:03 上午
 */
public class 剑指_Offer_03_数组中重复的数字 {

    public int findRepeatNumber(int[] nums) {
        Arrays.sort(nums);
        int length = nums.length;
        int pre = nums[0];
        for (int i = 1; i < length; i++) {
            if (nums[i] == pre) {
                return pre;
            } else {
                pre = nums[i];
            }
        }
        return -1;
    }


    public int findRepeatNumber2(int[] nums) {
        int length = nums.length;
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < length; i++) {
            if (set.contains(nums[i])) {
                return nums[i];
            } else {
                set.add(nums[i]);
            }
        }
        return -1;
    }
}
